package com.sevencar.activity;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.example.lightble.R;
import com.example.lightble.db.SettingDatabase;
import com.sevencar.activity.GroupItem.onCheckedListener;
import com.sevencar.activity.GroupItem.onDeleteBtnListener;

public class LightGroupsActivity extends Activity implements onCheckedListener,onDeleteBtnListener{

	List<Map<String, String>> groupNameList;
	LinearLayout mLinearLayout;
	
		
	int cnt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.light_group_layout);
		
		findViewById(R.id.id_light_group_add).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAddActivity();
			}
		});
		mLinearLayout=(LinearLayout)findViewById(R.id.id_light_group_linearlayout);
		
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		SettingDatabase sdb = new SettingDatabase(this,0);
		groupNameList=sdb.query_maplist("select distinct group_code, group_name from table_group ");
		sdb.close();
		
		mLinearLayout.removeAllViews();
		for(int i=0;i<groupNameList.size();i++)
		{
			GroupItem item=new GroupItem(LightGroupsActivity.this, groupNameList.get(i).get("group_name"),"");
			item.setOnCheckedListener(this);
			item.setOnDeleteBtnListener(this);
			mLinearLayout.addView(item);
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLinearLayout.removeAllViews();
	}
	private void showAddActivity()
	{
		startActivity(new Intent(this,LightGroupAddActivity.class));
	}
	
	/**
	 * 选中当前的,把别的选勾去掉.
	 */
	@Override
	public void onCheckedChange(GroupItem view, boolean isChecked) 
	{
		for(int i=0;i<mLinearLayout.getChildCount();i++)
		{
			GroupItem item=(GroupItem)mLinearLayout.getChildAt(i);
			if(item.equals(view)==false && isChecked){
				item.cancelChecked();
			}
		}
	}
	@Override
	public void onDelete(GroupItem view) {
		for(int i=0;i<mLinearLayout.getChildCount();i++)
		{
			GroupItem item=(GroupItem)mLinearLayout.getChildAt(i);
			if(item.equals(view)){
				mLinearLayout.removeView(view);
			}
		}
	}
	
	/**
	 * intent.putExtra("path", videoPath);
		intent.setClass(Main.this, FileShow.class);
		startActivityForResult(intent, 4);
	 */
}
